package com.kcnet.qk.controller.pyq;

import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.jesse.nativelogger.NLogger;

/**
 * Created by Administrator on 2018/4/25 0025.
 * 发送朋友圈图文
 */


public class PyqSendFriendTextAndImg extends BasePeopleController {

    public boolean init_imm;
    private static String TAG = "发送朋友圈图文";
    private String SendText = "测试文本"; //发送的文本
    private static PyqSendFriendTextAndImg mRongCloudInstance;

    private int imgSize = 3; //选择图片数量
    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static PyqSendFriendTextAndImg getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (PyqSendFriendTextAndImg.class) {

                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new PyqSendFriendTextAndImg();
                }
            }
        }

        return mRongCloudInstance;
    }


    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();

        mType = event.getEventType();

        if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.LauncherUI)) {
            NLogger.d(TAG,"1.进入微信首页 点击 发现 按钮");
            sleepTime(2000);
            tagTextClick("发现");

            sleepTime(1000);
            NLogger.d(TAG,"2.到达发现界面 点击 朋友圈");
            tagTextClick("朋友圈");
        }
        else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.tencent.mm.plugin.sns.ui.SnsTimeLineUI")) {
            NLogger.d(TAG, "3、到达朋友圈主页面，点击拍照分享");
            RootShell.click(456,38);
        }

        else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.tencent.mm.ui.base.k")) {
            NLogger.d(TAG, "4、发布朋友圈弹窗，点击弹出框的从相册选择");
            tagTextClick("从相册选择");
        }

     else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.tencent.mm.plugin.gallery.ui.AlbumPreviewUI")) {
            NLogger.d(TAG, "5、到达相册，准备选择图片");
            //选择完成
            /*临时将z加1，待修改*/
            if (tagIdClick("com.tencent.mm:id/bt3", imgSize + 1)) {
                tagIdClick("com.tencent.mm:id/hg");
            }
        } else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.tencent.mm.plugin.sns.ui.SnsUploadUI")) {
            NLogger.d(TAG, "6、到达发表界面");
            RootShell.click(100,115);
            sleepTime(1000);
            inputSayHiSentence(SendText);
            NLogger.d(TAG, "7、点击发表");
            sleepTime(1000);
            RootShell.click(450,38);
            stopTask();

        }
        else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.DialogUI)) {
            if(!tagTextClick("确定")){
                tagTextClick("我知道了");
            }
        }
    }

    public void begin() {

        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, "android.widget.GridView");
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                    return;
                }

                for (int q = 0; q < scrllorNodeInfo.getChildCount(); q++) {

                    if (currentAddCount == imgSize) {
                        currentAddCount = 0;
                        NLogger.d(TAG, "5.图片选择完成 点击完成");
                        tagTextClick("完成");
                        break;
                    }
                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(q);
                    sleepTime(1000);
                    currentAddCount++;
                    NLogger.d(TAG, "选择图片" + currentAddCount + " statsu:" + NodeUtil.performClick(as)+" a:"+as.getChildCount());

                    if (q == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }

            }
        }
    }

    /**
     *
     * @param minAddTime
     * @param maxAddTime
     * @param SendText 发布的文本
     * @param imgSize  图片数量
     */
    public void setParms(int minAddTime, int maxAddTime,String SendText,int imgSize) {
        //  stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.SendText = SendText;
        this.imgSize = imgSize;
    }


}
